<template>
  <view :class="['login-container']">

    <div style="display: flex;align-items: center;justify-content: center;">
      <image src="/static/logo.jpeg" style="width: 160rpx;height: 160rpx"></image>
    </div>
    <div class="content">
      <div class="login-title font-28">鸡鲜生生鲜</div>
      <div class="login-form">
        <div class="login-form-item">
          <image class="w-16 h-16" src="@/static/image/user.png"></image>
          <div style="flex-grow: 1">
          <u-input type="number" v-model="loginForm.username" placeholder="请输入账号"></u-input>
          </div>
        </div>
        <div class="login-form-item">
          <image class="w-14 h-16" src="@/static/image/lock.png"></image>
          <div style="flex-grow: 1">
            <u-input v-model="loginForm.password" :type="type" placeholder="请输入密码"></u-input>
          </div>
        </div>
        <div class="code-item" style="display: flex;align-items: center">
          <div style="flex-grow: 1">
            <div class="login-form-item" style="margin-top: 0">
              <image class="w-14 h-16" src="@/static/image/lock.png"></image>
              <u-input v-model="code" placeholder="请输入验证码"></u-input>
            </div>
          </div>
          <div class="code">
            <Identify ref="verificationRef"></Identify>
          </div>
        </div>
        <div class="button-item font-18" @click="login">
          注册
        </div>
        <div class="tips" @click="toReg">有账号？去登录</div>
      </div>
    </div>
  </view>
</template>

<script>
import Identify from "../../components/code.vue";
export default{
  components: {Identify},
  data(){
    return {
      type: 'password',
      loginForm: {
        username: '',
        password: ''
      },
      code: '',
    }
  },
  onLoad(){
    this.$store.commit('SET_USERINFO', {})
  },
  methods: {
    login() {
      if (!this.loginForm.username) {
        uni.showToast({
          title: '请输入账号',
          icon: 'none'
        })
        return
      }
      if (!this.loginForm.password) {
        uni.showToast({
          title: '请输入密码',
          icon: 'none'
        })
        return
      }
      // if (!this.code) {
      //   uni.showToast({
      //     title: '请输入验证码',
      //     icon: 'none'
      //   })
      //   return
      // }
      // if (!this.$refs.verificationRef.verification(this.code)) {
      //   uni.showToast({
      //     title: '请输入正确验证码',
      //     icon: 'none'
      //   })
      //   return
      // }
      uni.showLoading({
        title: '登录中...'
      })
      this.$api.register({
        username: this.loginForm.username,
        password: this.loginForm.password,
        role: 'user'
      }).then(res => {
        uni.hideLoading()
        this.$store.commit('SET_USERINFO', res)
        uni.reLaunch({
          url: '/pages/index/index'
        })
      })
    },
    toReg() {
      uni.navigateTo({
        url: '/pages/user/login'
      })
    }
  }
}
</script>

<style lang='scss'>
.login-container {
  position: relative;
  background-color: #ffffff;
  height: 100%;
  .tips {
    text-align: center;
    line-height: 88rpx;
    color: #666666;
  }
  .button-item {
    height: 100rpx;
    margin-top: 100rpx;
    border-radius: 50rpx;
    opacity: 1;
    background: #f15929;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    line-height: 100rpx;
    text-align: center;
  }
  .bg {
    position: absolute;
    left: -50px;
    top: -70px;
    width: 518px;
    height: 352px;
    opacity: 1;
  }
  .content {
    .login-form {
      padding: 0 60rpx;
      .code {
        flex-shrink: 0;
        margin-left: 20rpx;
        height: 100rpx;
        border-radius: 22rpx;
        overflow: hidden;
      }
      .code-item{
        height: 100rpx;
        margin-top: 40rpx;
        display: flex;
        align-items: center;
      }
      .login-form-item {
        height: 100rpx;
        margin-top: 40rpx;
        border-radius: 22rpx;
        opacity: 1;
        background: #F3F6FF;
        padding: 0 40rpx;
        display: flex;
        align-items: center;
        image {
          margin-right: 20rpx;
        }
      }
    }
    .change-box {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .login-title {
      font-family: PingFang SC;
      text-align: center;
      width: 100%;
      color: #f15929;
      line-height: 72px;
    }
    .change_icon {
      margin-right: 10rpx;
    }
    .sub-title {
      color: #8B8B8B;
      line-height: 24px;
      text-align: center;
    }
  }
}
</style>
